# encoding: utf-8
class Report::CulturalHeritageReportController < ApplicationController
  def show_pdf
    #<%= link_to 'Exportar a PDF',{:controller => '/report/cultural_heritage_report', :action => 'show_pdf',:id => @cultural_heritage_cultural_heritage.id}%>

    selected_cultural_heritage = CulturalHeritage::CulturalHeritage.find(params[:id])
    
    pdf = Prawn::Document.new

    pdf.image "#{RAILS_ROOT}/public/images/pukara_logo.png", :width => 80, :at => [0, 715]
    pdf.text_box "Sistema de información cultural pukara",
      :at => [100, 715],
      :width => 430
    pdf.text_box "Ficha de patrimonio cultural - " << selected_cultural_heritage.nationalRegistrationCode,
      :at => [100, 690],
      :width => 430,
      :style => :bold,
      :size => 20
    pdf.text_box selected_cultural_heritage.name,
      :at => [100, 655],
      :width => 430,
      :style => :bold,
      :size => 20

    if !(selected_cultural_heritage.avatar.url.blank? || selected_cultural_heritage.avatar_file_name.blank?)
      if ((selected_cultural_heritage.avatar_file_name.include?('.jpg')) || (selected_cultural_heritage.avatar_file_name.include?('.png')))
        pdf.image ("#{RAILS_ROOT}" << "/public" << selected_cultural_heritage.avatar.url).split("?").at(0), :height => 200, :position => :center, :vposition => 130
      else
        pdf.image "#{RAILS_ROOT}/public/images/pukara_logo.png", :height => 200, :position => :center, :vposition => 130
      end
    else
      pdf.image "#{RAILS_ROOT}/public/images/pukara_logo.png", :height => 200, :position => :center, :vposition => 130
    end

    pdf.move_down 160

    pdf.text "<b><u>I. DESCRIPCION GENERAL</u></b>",
      :inline_format => true
    pdf.move_down 10
    pdf.text selected_cultural_heritage.description
    pdf.move_down 20

    pdf.text "<b><u>II. MEDIDAS</u></b>",
      :inline_format => true
    pdf.move_down 10
    
    arr = Array.new
    arr[0] = Array.new
    arr[1] = Array.new
    arr[2] = Array.new
    arr[3] = Array.new

    if (selected_cultural_heritage.group == 1)
      arr[0][0] = "Alto (cm)"
      arr[0][1] = "Largo (cm)"
      arr[0][2] = "Ancho (cm)"
      arr[0][3] = "Peso (kg)"
    else
      arr[0][0] = "Alto (m)"
      arr[0][1] = "Largo (m)"
      arr[0][2] = "Ancho (m)"
      arr[0][3] = "Peso (ton)"
    end

    arr[1][0] = selected_cultural_heritage.height.to_s
    arr[1][1] = selected_cultural_heritage.long.to_s
    arr[1][2] = selected_cultural_heritage.width.to_s
    arr[1][3] = selected_cultural_heritage.weight.to_s

    pdf.table arr, :column_widths => [135, 135, 135, 135], :row_colors => ["F0F0F0", "FFFFFF"], :header => true do
      row(0).border_width = 2
      row(0).font_style = :bold
    end

    pdf.move_down 20

    pdf.text "<b><u>III. MANEJO DE ESTADOS</u></b>",
      :inline_format => true
    pdf.move_down 10

    arr = Array.new
    arr[0] = Array.new
    arr[1] = Array.new
    arr[2] = Array.new
    arr[0][0] = "Estado"
    arr[0][1] = "Estado de conservación"
    arr[0][2] = "Estado de integridad"
    arr[1][0] = selected_cultural_heritage.str_state
    arr[1][1] = selected_cultural_heritage.obj_conservational_state.name
    arr[1][2] = selected_cultural_heritage.obj_integrity_state.name

    pdf.table arr, :column_widths => [180, 180, 180], :row_colors => ["F0F0F0", "FFFFFF"], :header => true do
      row(0).border_width = 2
      row(0).font_style = :bold
    end

    pdf.move_down 20

    pdf.text "<b><u>IV. AGRUPACIÓN</u></b>",
      :inline_format => true
    pdf.move_down 10

    arr = Array.new
    arr[0] = Array.new
    arr[1] = Array.new
    arr[2] = Array.new
    arr[0][0] = "Grupo"
    arr[0][1] = "Sub-grupo"
    arr[0][2] = "Tipología"
    arr[1][0] = selected_cultural_heritage.obj_group.name
    arr[1][1] = selected_cultural_heritage.obj_subGroup.name
    arr[1][2] = selected_cultural_heritage.obj_tipology.name

    pdf.table arr, :column_widths => [180, 180, 180], :row_colors => ["F0F0F0", "FFFFFF"], :header => true do
      row(0).border_width = 2
      row(0).font_style = :bold
    end

    arr = Array.new
    arr[0] = Array.new
    arr[1] = Array.new
    arr[0][0] = "Periodo"
    arr[0][1] = "Cultura"
    arr[1][0] = selected_cultural_heritage.obj_period.name

    if selected_cultural_heritage.obj_culture.nil?
      arr[1][1] = 'No se indica.'
    else
      arr[1][1] = selected_cultural_heritage.obj_culture.name
    end


    pdf.table arr, :column_widths => [270, 270], :row_colors => ["F0F0F0", "FFFFFF"], :header => true do
      row(0).border_width = 2
      row(0).font_style = :bold
    end

    pdf.move_down 20

    pdf.text "<b><u>V. MATERIALES</u></b>",
      :inline_format => true
    pdf.move_down 10

    pdf.text selected_cultural_heritage.materials

    pdf.move_down 20

    @contained_cultural_heritages = CulturalHeritage::CulturalHeritage.all
    if !(@contained_cultural_heritages.blank?)
      pdf.text "<b><u>VI. ASIGNACIONES</u></b>",
        :inline_format => true
      pdf.move_down 10

      arr = Array.new
      arr[0] = Array.new
      arr[0][0] = "Código INC"
      arr[0][1] = "Nombre"
      p = 1

      @contained_cultural_heritages.each do |heritage|
        if (heritage.father_maximum_id == selected_cultural_heritage.id)
          arr[p] = Array.new
          arr[p][0] = heritage.nationalRegistrationCode
          arr[p][1] = heritage.name
          p = p + 1
        end
      end

      if arr.size > 1
        pdf.table arr, :column_widths => [100, 440], :row_colors => ["F0F0F0", "FFFFFF"], :header => true do
          row(0).border_width = 2
          row(0).font_style = :bold
        end
      else
        pdf.text "No se tiene asignaciones."
      end

    end

    pdf.repeat(:all, :dynamic => true) do
      pdf.draw_text Time.now.strftime("%d-%m-%Y").to_s, :at => [0, 0]
      pdf.draw_text pdf.page_number, :at => [530, 0]
    end   

    send_data pdf.render, :filename => "Ficha - " << selected_cultural_heritage.nationalRegistrationCode << ".pdf", :type => "application/pdf"
  end
end

